package com.tanhua.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {


    @Reference
    private SettingsApi settingsApi;

    @Reference
    private QuestionApi questionApi;

    @Reference
    private BlackListApi blackListApi;



    //查找用户的设置
    public ResponseEntity querySettings() {
        //1. 获取登陆的用户
        User user = UserHolder.get();
        //2. 查找该用户对应的Settings
        Settings settings  =settingsApi.findByUserId(user.getId());
        //3. 查找该用户的陌生人问题
        Question question = questionApi.findByUserId(user.getId());

        //4. 创建Vo对象
        SettingsVo settingsVo = new SettingsVo();
        if(settings!=null){
            BeanUtils.copyProperties(settings,settingsVo);
        }
        if(question!=null){
            settingsVo.setStrangerQuestion(question.getTxt());
        }
        settingsVo.setPhone(user.getMobile());
        //5. 返回对象
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置用户通知保存
     * @param settings  用户提交的设置通知对象
     * @return
     */
    public ResponseEntity saveNotification(Settings settings) {
        //1. 获取登陆用户
        Long userId = UserHolder.getUserId();

        //2. 根据用户id查找该用户的通知设置
        Settings dbSettings = settingsApi.findByUserId(userId);

        //3. 如果没有该用户的通知设置，代表该用户是第一次设置，需要做保存动作
        if(dbSettings==null){
            settings.setUserId(userId);
            settingsApi.save(settings);
        }else {
            settings.setId(dbSettings.getId());
            //4. 如果有该用户的通知设置，那么该用户就是做更新操作
            settingsApi.update(settings);
        }
        //5. 返回响应
        return ResponseEntity.ok(null);
    }

    /**
     * 设置陌生人问题
     * @param content
     * @return
     */
    public ResponseEntity saveQuestion(String content) {
        //1. 获取登陆用户
        Long userId = UserHolder.getUserId();

        //2. 根据用户的id查找陌生人问题
        Question question = questionApi.findByUserId(userId);

        //3.判断是否为空
        if(question==null){
            //4. 如果为空则为第一次设置陌生人问题
            question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.save(question);
        }else {
            //5.如果不为空则是更新
            question.setTxt(content);
            questionApi.update(question);
        }
        //6. 返回响应
        return ResponseEntity.ok(null);
    }

    //查询黑名单列表
    public ResponseEntity blacklist(Integer pageNum, Integer pagesize) {
        //1. 得到当前登陆的用户对象
        User user = UserHolder.get();
        //2. 根据用户的id查询Page对象
        Page<UserInfo> page = blackListApi.findBlackList(pageNum,pagesize,user.getId());

        //3. 把结果转换为PageResult对象
        PageResult pageResult = new PageResult(pageNum,pagesize,(int)page.getTotal(),page.getRecords());


        //4. 把响应结果返回
        return ResponseEntity.ok(pageResult);

    }


    /**
     * 作用： 移除黑名单
     * @param blackUserId
     * @return
     */
    public ResponseEntity deleteBlackUser(Long blackUserId) {
        //1.得到UserId
        Long userId = UserHolder.getUserId();
        //2. 删除userid, blackUserId
        blackListApi.deleteBlackUser(userId,blackUserId);
        //3. 返回响应
        return ResponseEntity.ok(null);
    }
}
